package com.dwyinyuan.consumer;

import com.dwyinyuan.task.DownloadFileTask;
import com.dwyinyuan.model.FileDTO;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;

public class FileConsumer implements Runnable {
    private BlockingQueue<FileDTO> blockingQueue;
    private ExecutorService taskPool;

    public FileConsumer(BlockingQueue<FileDTO> blockingQueue, ExecutorService taskPool) {
        this.blockingQueue = blockingQueue;
        this.taskPool = taskPool;
    }

    @Override
    public void run() {
        try {
            FileDTO fileDTO = null;
            while ((fileDTO = blockingQueue.take()).getHasNext()) {
                taskPool.submit(new DownloadFileTask(fileDTO));
            }
            System.out.println("file consumer is over");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
